//var serverUrl = 'http://localhost:8100/vehiclecheck';
//var serverUrl = 'http://10.12.49.102:8080/vehiclecheck';
var serverUrl =  'http://vehiclecheck.chinacloudapp.cn/vehiclecheck';
angular.module('_vehicleData',[]).factory('_vehicleData', ['$http' ,'$q', '_DBSerive', _vehicleData]);
function _vehicleData($http, $q, _DBSerive) {
	var Luser={
		name:'name',
		password:'111'
	}
	//用户登录
	function login(user){
		var deferred = $q.defer();
		debugger;
		// $http.post(serverUrl+'/login.do',{
		// 	'actno':user.username,'pwd':user.password
		// }).
		// success(function(data){
		// 	deferred.resolve(data);
		// 	console.log(data);
		// }).error(function(data,status){
		// 	deferred.reject(status);
		// });
		if(Luser.name==user.username&&Luser.password==user.password){
				// deferred.resolve(data);
				console.log('success');

		}else{
			deferred.reject(status);
		}
		return deferred.promise;
	}
	//获取所有的用户数据
	function getAllUser(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/user/queryAll.do').
		success(function(data){
			deferred.resolve(data);
			console.log(data);
		}).error(function(){
			deferred.reject();
		});
		return deferred.promise;
	}
	//获取所有的运输公司数据
	function getTransporter(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/transportco/queryAll.do')
			.success(function(data) {
				deferred.resolve(data);
				console.log(data);
			})
			.error(function() {
				deferred.reject();
			});
		return deferred.promise;
	};
	//获取所有检查项信息
	function getAllCheckItem(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/checkitem/queryAll.do').
		success(function(data){
			deferred.resolve(data);
			console.log(data);			
		})
		.error(function() {
			deferred.reject();
		});
		return deferred.promise;
	}
	//获取所有检查项-车型关联信息
	function getAllCheckItemCarType(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/checkitem/queryCheckItemCarType.do').
		success(function(data){
			deferred.resolve(data);
			console.log(data);		
		})
		.error(function() {
			deferred.reject();
		});
		return deferred.promise;
	}
	//获取所有车型信息
	function getAllCarType(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/cartype/queryAll.do').
		success(function(data){
			deferred.resolve(data);
			console.log(data);			
		})
		.error(function() {
			deferred.reject();
		});
		return deferred.promise;
	}
	//获取所有仓库信息
	function getAllWarehouse(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/store/queryAll.do').
		success(function(data){
			deferred.resolve(data);
			console.log(data);			
		})
		.error(function() {
			deferred.reject();
		});
		return deferred.promise;
	}
	//获取所有仓库-车型关联信息
	function getAllWarehouseCarType(){
		var deferred = $q.defer();
		$http.post(serverUrl+'/store/queryStoreCar.do').
		success(function(data){
			deferred.resolve(data);
			console.log(data);			
		})
		.error(function() {
			deferred.reject();
		});
		return deferred.promise;
	}
	
	//基本信息提交
	function submitCheckItem(itemInfo){
		var deferred = $q.defer();
		var checkItems = [];
		angular.forEach(itemInfo.checkItems,function(item){
			var checkItem ={};
			checkItem.answer = item.answer;
			checkItem.checkitemid = item.checkitemid;
			checkItems.push(checkItem);
		});
        console.log("score:"+parseInt(itemInfo.percent));
		var data = {storecode:itemInfo.warehouse.storecode, carno:itemInfo.carno, score:parseInt(itemInfo.percent),actno:itemInfo.actno, createtime:itemInfo.submitTime, ispass:itemInfo.ispass, cartype:itemInfo.carType.cartypename, sealno:itemInfo.sealno, company:itemInfo.transporter.companyname, checkItems:checkItems};
		$http.post(serverUrl+'/checkRecords/commitRecord.do', data).
		success(function(data){
			deferred.resolve(data);
		})
		.error(function(){
			deferred.reject();
		});
		return deferred.promise;
	}

    //照片上传
    function uploadPic(param){
        var uploadUrl = serverUrl + '/phone/picupload?recorditemid='+param.recorditemid;
        //上传照片
        var options = new FileUploadOptions();
        options.fileKey = "file";
        options.fileName = param.filename;
        options.mimeType = "image/jpeg";
        options.httpMethod = "GET";
        options.params = param;
        var ft = new FileTransfer();
        ft.upload(param.photo, encodeURI(uploadUrl), function(result){
            //上传图片成功
            console.log(result);
        }, function(errMsg){
            console.log(errMsg);
            _$toast.showShortCenter(errMsg);
        }, options);
    }

    //修改密码
    function changePwd(user){
        var deferred = $q.defer();
        $http.post(serverUrl+'/user/updatePwd.do',{
            'userCode':user.userCode,
            'oldPwd':user.oldPwd,
            'newPwd':user.newPwd
        }).
            success(function(data){
                deferred.resolve(data);
                console.log(data);
            }).error(function(data,status){
                deferred.reject(status);
            });
        return deferred.promise;
    }
	
	//获取所有数据
	function getAllData(){
		var num = 6;
		var count = 0;
		var deferred = $q.defer();
		getTransporter().then(function(data){
			console.log(1);
			count++;
			if(count == num) deferred.resolve();
			_DBSerive.insertTransport(data.transportCOList);
		});
		getAllCheckItem().then(function(data){
			console.log(2);
			count++;
			if(count == num) deferred.resolve();
			_DBSerive.insertChenkItem(data.checkItemList);
		});
		getAllCheckItemCarType().then(function(data){
			console.log(3);
			count++;
			if(count == num) deferred.resolve();
			_DBSerive.insertCheckItemCarType(data.CheckItemCarTypeList);
		});
		getAllCarType().then(function(data){
			console.log(4);
			count++;
			if(count == num) deferred.resolve();
			_DBSerive.insertCarType(data.cartyprList);
		});
		getAllWarehouse().then(function(data){
			console.log(5);
			count++;
			if(count == num) deferred.resolve();
            console.log(data.storeList);
			_DBSerive.insertWarehouse(data.storeList);
		});
		getAllWarehouseCarType().then(function(data){
			console.log(6);
			count++;
			if(count == num) deferred.resolve();
			_DBSerive.insertWarehouseCarType(data.StoreCarTypeList);
		});
		return deferred.promise;
	}

	return {
		login: login,
		getAllUser: getAllUser,
		getTransporter: getTransporter,
		getAllCheckItem: getAllCheckItem,
		getAllCheckItemCarType: getAllCheckItemCarType,
		getAllCarType: getAllCarType,
		getAllWarehouse: getAllWarehouse,
		getAllWarehouseCarType: getAllWarehouseCarType,
		getAllData: getAllData,
		submitCheckItem:submitCheckItem,
        changePwd:changePwd,
        uploadPic:uploadPic
	};
	
};